﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Story.WinForms;
using Story.Framework.WinUI;
using DevExpress.XtraTab;
using DevExpress.XtraEditors;
using DevExpress.XtraBars.Docking;
using Storys.Collect.WinUI;
using Story.Framework;

namespace SRAppClient
{
    public partial class MainForm : SRRibbonForm,IMainForm
    {
        public MainForm()
        {
            AppDomain.CurrentDomain.UnhandledException += ErrorForm.OnUnhandledException;
            Application.ThreadException += ErrorForm.OnThreadExceptionEventHandler;
            InitializeComponent();
            AppClient.MainForm = this;

            ServerConfiguration.ConfigXml(AppDomain.CurrentDomain.BaseDirectory + "server.config");
        }

        #region IMainForm 成员

        public DateTime ServerTime
        {
            get { return DateTime.Now; }
        }

        private Dictionary<string, PageBase> m_Pages = new Dictionary<string, PageBase>();
        public Dictionary<string, PageBase> Pages
        {
            get
            {
                return m_Pages;
            }
        }
        private Dictionary<string, DockPanelBase> m_Docks = new Dictionary<string, DockPanelBase>();
        public Dictionary<string, DockPanelBase> Docks
        {
            get { return m_Docks; }
        }

        public void ShowPage(PageBase p)
        {
            if (string.IsNullOrEmpty(p.Title))
            {
                ShowMsg("该页面没有设置标题，无法打开!", "页面载入错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (m_Pages.ContainsKey(p.Title))
            {
                foreach (XtraTabPage tp in mainTableControl.TabPages)
                {
                    if (p.Title == tp.Text)
                    {
                        mainTableControl.MakePageVisible(tp);
                        mainTableControl.SelectedTabPage = tp;
                        return;
                    }
                }
            }
            else
            {
                XtraTabPage tp = new XtraTabPage();
                m_Pages.Add(p.Title, p);
                p.Dock = DockStyle.Fill;
                tp.Controls.Add(p);
                tp.Text = p.Title;
                mainTableControl.TabPages.Add(tp);
                mainTableControl.MakePageVisible(tp);
                mainTableControl.SelectedTabPage = tp;

            }
        }

        public void ShowPage(string typeInfoStr)
        {
            ShowPage(typeInfoStr, null);
        }

        public void ShowPage(string typeInfoStr, string paramsStr)
        {
            PageBase p = PageBase.Create(typeInfoStr, paramsStr);
            ShowPage(p);
        }

        public void ClosePage(PageBase p)
        {
            foreach (XtraTabPage tp in mainTableControl.TabPages)
            {
                if (tp.Text == p.Title)
                {
                    mainTableControl.TabPages.Remove(tp);
                    m_Pages.Remove(p.Title);
                    return;
                }
            }
        }

        public DialogResult ShowMsg(string text)
        {
            return XtraMessageBox.Show(text);
        }

        public DialogResult ShowMsg(string text, string caption)
        {
            return XtraMessageBox.Show(text,caption);
        }

        public DialogResult ShowMsg(string text, string caption, bool isYesNo)
        {
            if (isYesNo)
                return XtraMessageBox.Show(text, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            else
                return XtraMessageBox.Show(text, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
        }

        public DialogResult ShowMsg(string text, string caption, MessageBoxButtons buttons)
        {
            return XtraMessageBox.Show(text, caption, buttons);
        }

        public DialogResult ShowMsg(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return XtraMessageBox.Show(text, caption, buttons, icon);
        }

        public DialogResult ShowMsg(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            return XtraMessageBox.Show(text, caption, buttons, icon, defaultButton);
        }

        public void OutMsg(string msg)
        {
            
        }
        #endregion

        private void barCheckItem1_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowDockPage("Storys.Collect.SiteDoc,Storys.Collect", "Title=网站任务");

        }



        #region IMainForm 成员
        DockPanel abc = new DockPanel();

        public void ShowDockPage(DockPanelBase d)
        {

            if (string.IsNullOrEmpty(d.Title))
            {
                ShowMsg("该页面没有设置标题，无法打开!", "页面载入错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DockPanel panel = null;
            if (manDockManager.Panels[d.XDockingStyle.ToString()] == null)
            {
                panel = manDockManager.AddPanel(d.XDockingStyle);
                panel.Name = d.XDockingStyle.ToString();
                panel.Text = d.Title;
                panel.Size = d.Size;
                d.Dock = DockStyle.Fill;
                m_Docks.Add(d.Title, d);
                panel.Controls.Add(d);
            }
            else
            {
                panel = manDockManager.Panels[d.XDockingStyle.ToString()];
                if (panel.ParentPanel == null && manDockManager.Panels[d.Title] == null && panel.Text!=d.Title)
                {
                    DockPanel p1 = panel.AddPanel();
                    p1.Text = d.Title;
                    p1.Name = d.Title;
                    p1.Size = d.Size;
                    d.Dock = DockStyle.Fill;
                    p1.Controls.Add(d);
                    m_Docks.Add(d.Title, d);
                    panel.ParentPanel.Tabbed = true;
                }
                else if (manDockManager.Panels[d.Title] == null && panel.Text != d.Title)
                {
                    DockPanel p1 = panel.ParentPanel.AddPanel();
                    p1.Text = d.Title;
                    p1.Name = d.Title;
                    p1.Size = d.Size;
                    d.Dock = DockStyle.Fill;
                    m_Docks.Add(d.Title, d);
                    p1.Controls.Add(d);
                }
                else
                {
                    if (manDockManager.Panels[d.Title] != null)
                    {
                        manDockManager.Panels[d.Title].Visible = true;
                        manDockManager.Panels[d.Title].Show();
                    }
                    else if (manDockManager.Panels[d.XDockingStyle.ToString()] != null)
                    {
                        manDockManager.Panels[d.XDockingStyle.ToString()].Visible = true;
                        manDockManager.Panels[d.XDockingStyle.ToString()].Show();
                    }
                }
            }

        }


        public void ShowDockPage(string typeInfoStr)
        {
            ShowDockPage(typeInfoStr, null);
        }

        public void ShowDockPage(string typeInfoStr, string paramsStr)
        {
            DockPanelBase dp = DockPanelBase.Create(typeInfoStr, paramsStr);
            ShowDockPage(dp);
        }

        public void CloseDockPage(DockPanelBase p)
        {
            throw new NotImplementedException();
        }

        #endregion

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Sites sites = new Sites();
           sites.XDockingStyle= DockingStyle.Left;
            ShowDockPage(sites);
        }

        private void barButtonItem26_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
         
        }

        //新建站点
        private void itemNewWeb_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowPage("Storys.Collect.WinUI.NewSitePage,Storys.Collect.WinUI");
        }

    }
}
